<?php
require_once 'InsuranceRule.class.php';
require_once 'RuleData.class.php';
class InsuranceRuleManager {
    private $rules =   array();
    const   RANK_I  =   "1";
    const   RANK_II =   "2";
    const   RANK_III =   "3";

    function __construct() {
    	$config       =   Celini::configInstance();
        $modulePaths  =   $config->get('module_paths');
        $cfgInsurance =   $config->get('insurance');
        $path    =   $modulePaths['billing'] . "/";
        $ruleFile    =   $path . "local/includes/Insurance/Data/rules.xml";
        // Convert the xml document to an xml object
        $xmlObject  =   simplexml_load_file($ruleFile);
        // Iterate throught the array of rules to create rule objects
        foreach ($xmlObject->children() as $rule) {
            $attributes =  $rule->attributes();
            $ruleName   =  strtolower($attributes['name']);
            $objInsuranceRule  =   new  InsuranceRule();
            // Iterate through items of the rule to create RuleData objects
            $minSalary          =   EnforceType::int($cfgInsurance['min_salary']);
            foreach ($rule->children() as $item) {
                $maxAmount  =   (float)$item->maxAmount * $minSalary;
            	$objRuleData   =   new RuleData((float)$item->percent, (float)$maxAmount, (int)$item->conditonRequired);
                $itemAttrs  =   $item->attributes();
                $itemType   =   (string)$itemAttrs['type'];
                switch ($itemType) {
                	case InsuranceRule::ITEM_TYPE_NORMAL:
                		$objInsuranceRule->setNormal($objRuleData);
                		break;
                	case InsuranceRule::ITEM_TYPE_HIGH_TECH:
                		$objInsuranceRule->setHighTech($objRuleData);
                        break;
                    case InsuranceRule::ITEM_TYPE_CANCER_DRUG:
                        $objInsuranceRule->setCancerDrug($objRuleData);
                        break;
                    case InsuranceRule::ITEM_TYPE_TRANSPORT:
                        $objInsuranceRule->setTransport($objRuleData);
                        break;
                    default:
                    	break;
                }
            }

            // Set the rule to the rule array
            $this->rules["$ruleName"]   =   $objInsuranceRule;
        }
    }

	/**
	 * Get an InsuranceRule object for given data from an examinaton.
	 *
	 * @param InsuranceInfo $objInsuranceInfo Object containing insurance info of the patient
	 * @param decimal $totalPrice Total price of the examination
	 */
	public function getInsuranceRule($objInsuranceInfo, $totalPrice) {
        // Config values
        $cfgInsurance =   Celini::configInstance()->get('insurance');
        $minSalary          =   EnforceType::int($cfgInsurance['min_salary']);
        $communeHospital    =   $cfgInsurance['commune_hospital'];
        $hospitalRank       =   $cfgInsurance['hospital_rank'];

        if ($objInsuranceInfo->getInsuranceGroup() === false) {
        	return false;
        }

        if ($objInsuranceInfo->isCorrectInsuranceRoute()) {
            if ($communeHospital || ($totalPrice < 15*$minSalary/100)) {
                return $this->rules['rule1'];
            }
            else {
                switch ($objInsuranceInfo->getInsuranceGroup()) {
                	case InsuranceInfo::TYPE_TE:
                		return $this->rules['rule2'];
                	case InsuranceInfo::TYPE_CC:
                		return $this->rules['rule3'];
                	case InsuranceInfo::TYPE_CK:
                    case InsuranceInfo::TYPE_CA:
                    	return $this->rules['rule4'];
                    case InsuranceInfo::TYPE_HT:
                        return $this->rules['rule5'];
                    case InsuranceInfo::TYPE_BT:
                    case InsuranceInfo::TYPE_HN:
                        return $this->rules['rule6'];
                    case InsuranceInfo::TYPE_CN:
                        return $this->rules['rule7'];
                    default:
                    	return $this->rules['rule8'];
                }
            }
        }
        else {
            if ($hospitalRank == self::RANK_III) {
            	return $this->rules['rule9'];
            }
            elseif ($hospitalRank == self::RANK_II) {
            	return $this->rules['rule10'];
            }
            elseif ($hospitalRank == self::RANK_I) {
            	return $this->rules['rule11'];
            }
        }
    }
}
?>